#include "Bullet.h"

Bullet::Bullet()
	: mActive(false) //why is this better?
{
}

Bullet::~Bullet()
{
}

void Bullet::Load()
{
	mSprite.Load("bullet1.png");
}

void Bullet::Unload()
{
	mSprite.Unload();
}

void Bullet::Update(float deltaTime)
{
	//only update active bullets
	if(mActive)
	{
		mPosition += mVelocity * deltaTime;
		const int kWinWidth = IniFile_GetInt("WinWidth", 800) + (mSprite.GetWidth() / 2);
		const int kWinHeight = IniFile_GetInt("WinHeight", 600) + (mSprite.GetHeight() /2);
		if(mPosition.x < 0 || mPosition.x > kWinWidth ||
			mPosition.y < 0|| mPosition.y > kWinHeight)
		{
			Kill();
		}
	}

}

void Bullet::Render()
{
	//only render active bullets
	if(mActive)
	{
		const int width = mSprite.GetWidth();
		const int height = mSprite.GetHeight();

		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset;

		mSprite.SetPosition(renderPosition);
		mSprite.Render();
	}
}

void Bullet::Fire(const SVector2& pos, const SVector2& vel)
{
	mPosition = pos;
	mVelocity = vel;
	mActive = true;
}

void Bullet::Kill()
{
	//set false to stop update and render functions
	mActive = false;
}